<template>
  <div>
     <Row>
       <label>渠道号:</label>
        <Select v-model="newPlayerType" style="width:200px" not-found-text="-1">
          <Option v-for="item in tableList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
        <Date-picker v-model="startDay" placeholder="开始日期" format="yyyy-MM-dd"></Date-picker>
        <Date-picker v-model="endDay" placeholder="结束日期" format="yyyy-MM-dd"></Date-picker>
        <Button type="primary" @click="onSearch">查询</Button>
        <Button type="primary" @click="onRefresh">刷新统计</Button>
    </Row>
    </br>
    <Table :border="true" :stripe="true" :show-header="true" :height="500" :data="tableData" :columns="tableColumns"></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
  </div>
</template>
<script>

import {getRoleKeepAnalysTest, refreshRoleKeepAnalys} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
export default {
  data () {
    return {
      tableData: [],
      startDay: '',
      endDay: '',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      newPlayerType: '-1',
      tableList: [
        {
          value: '-1',
          label: '全测试渠道'
        },
        {
          value: '1',
          label: '1'
        },
        {
          value: '2',
          label: '2'
        },
        {
          value: '3',
          label: '3'
        },
        {
          value: '4',
          label: '4'
        },
        {
          value: '5',
          label: '5'
        },
        {
          value: '6',
          label: '6'
        },
        {
          value: '7',
          label: '7'
        },
        {
          value: '8',
          label: '8'
        },
        {
          value: '9',
          label: '9'
        },
        {
          value: '10',
          label: '10'
        },
        {
          value: '11',
          label: '11'
        },
        {
          value: '12',
          label: '12'
        },
        {
          value: '13',
          label: '13'
        },
        {
          value: '14',
          label: '14'
        },
        {
          value: '15',
          label: '15'
        },
        {
          value: '16',
          label: '16'
        },
        {
          value: '17',
          label: '17'
        },
        {
          value: '18',
          label: '18'
        }
      ]
    }
  },
  created () {
    this.endDay = new Date()
    this.startDay = new Date().setDate(new Date().getDate() - 7)
    this.onSearch()
  },
  methods: {
    onSearch () {
      let sSendDay = 0
      let eSendDay = 0
      if (this.startDay !== '') {
        let sDay = new Date(this.startDay)
        sSendDay = '' + sDay.getFullYear() + ((sDay.getMonth() + 1) < 10 ? '0' + (sDay.getMonth() + 1) : (sDay.getMonth() + 1)) + (sDay.getDate() < 10 ? '0' + sDay.getDate() : sDay.getDate())
        sSendDay = parseInt(sSendDay)
      }
      if (this.endDay !== '') {
        let eDay = new Date(this.endDay)
        eSendDay = '' + eDay.getFullYear() + ((eDay.getMonth() + 1) < 10 ? '0' + (eDay.getMonth() + 1) : (eDay.getMonth() + 1)) + (eDay.getDate() < 10 ? '0' + eDay.getDate() : eDay.getDate())
        eSendDay = parseInt(eSendDay)
      }

      let type = parseInt(this.newPlayerType)
      console.log('newPlayerType:', this.newPlayerType, ',sSendDay:', sSendDay, ',eSendDay:', eSendDay, ',crtPage:', this.crtPage, ',pageSize:', this.pageSize)
      getRoleKeepAnalysTest(sSendDay, eSendDay, type, this.crtPage, this.pageSize).then((data) => {
        if (!data) {
          return
        }
        this.tableData = []
        let RoleKeepAnalysVO = getVO('RoleKeepAnalysVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = RoleKeepAnalysVO.decode(pv.data[i])
          vo.keepRate1 = vo.keepRate1
          vo.keepRate3 = vo.keepRate3
          vo.keepRate7 = vo.keepRate7
          vo.keepRate14 = vo.keepRate14
          vo.keepRate30 = vo.keepRate30
          this.tableData.push(vo)
        }
        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onRefresh () {
      refreshRoleKeepAnalys().then((data) => {
        if (!data) {
          return
        }
        this.$Message.info('刷新成功!!!')
        console.log('refresh role keep analys end')
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: '时间',
          key: 'day',
          align: 'center'
        },
        {
          title: '测试类型',
          key: 'newPlayerType',
          align: 'center'
        },
        {
          title: '登录人数',
          key: 'loginCount',
          align: 'center'
        },
        {
          title: '新增人数',
          key: 'addCount',
          align: 'center'
        },
        {
          title: '次留存',
          key: 'keepRate1',
          align: 'center'
        },
        {
          title: '3留存',
          key: 'keepRate3',
          align: 'center'
        },
        {
          title: '7留存',
          key: 'keepRate7',
          align: 'center'
        },
        {
          title: '14留存',
          key: 'keepRate14',
          align: 'center'
        },
        {
          title: '30留存',
          key: 'keepRate30',
          align: 'center'
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}
</style>
